const fs = require('fs');
const co = require('co');
const path = require('path');
const oss = require('ali-oss');
const config = require('./deploy-config');
//构建oss对象
const store = oss(config.oss);

(()=>{
  const root = path.resolve(__dirname, config.ossPath.upload);
  const files = [];
  //递归取出所有文件夹下所有文件的路径
  function readDirSync(p) {
    const pa = fs.readdirSync(p);
    pa.forEach((e) => {
      const cur_path = `${p}/${e}`;
      const info = fs.statSync(cur_path);
      if (info.isDirectory()) {
        readDirSync(cur_path);
      } else {
        files.push(cur_path);
      }
    });
  }
  readDirSync(root);

  co(function* () {
    let result, e;
    // 先清空目录
    // result = yield store.deleteMulti([config.ossPath.ossRoot + '/js/*.js']);
    // console.log(result);
    
    // console.log(`删除 ${result.name}: ${result.res.status}`);
    //遍历文件
    for (let index = 0; index < files.length; index += 1) {
      e = files[index];
      // console.log(config.ossPath.ossRoot + e.replace(root, ''));
      result = yield store.put(config.ossPath.ossRoot + e.replace(root, ''), e);
      //提交文件到oss，这里要注意，阿里云不需要创建新文件夹，只有有路径，没有文件夹会自动创建
      console.log(`${result.name}: ${result.res.status}`);
    }
  });
})()